<template>
  <!--  人员产量汇总-->
  <div class="content">
    <div class="pageSearchView">
      <el-form :inline="true" :model="searchParam" class="demo-form-inline">
        <el-form-item label="工厂/车间" style="width: 250px">
          <el-tree-select v-model="searchParam.organizationId" :data="workRoomList" check-strictly
                          :props="{ children: 'sonList', label: 'orgName', value: 'id' }"
                          @change="handleOrganizationChange"/>
        </el-form-item>
        <el-form-item label="班组编码" style="width: 250px">
          <el-select v-model="searchParam.ids" placeholder="请选择班组编码" clearable filterable @change="handleTeamChange">
            <el-option v-for="item in teamData" :key="item.id" :label="item.code" :value="item.id"/>
          </el-select>
        </el-form-item>
        <el-form-item label="人员" style="width: 250px" required>
          <el-select v-model="searchParam.userId" placeholder="请选择人员" clearable filterable>
            <el-option v-for="item in teamPeopleData" :key="item.userId" :label="item.userName" :value="item.userId"/>
          </el-select>
        </el-form-item>
        <el-form-item label="物料编码" style="width: 250px">
          <el-input v-model="searchParam.materialNo" placeholder="请输入物料编码" clearable/>
        </el-form-item>
        <el-form-item label="时间区间" prop="times" style="width: 450px" required>
          <el-date-picker v-model="searchParam.times" type="datetimerange" start-placeholder="开始时间"
                          end-placeholder="结束时间" value-format="YYYY-MM-DD HH:mm:ss"/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :icon="Search" @click="getList()">查询</el-button>
          <el-button class="refreshBtn" :icon="Refresh" @click="resetSearch()">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="pageTableView">
      <div class="pageTableMenuView">
        <div class="pageTableTitle">列表信息</div>
      </div>
      <!-- 表格 -->
      <el-table class="pageTableClass" :data="paginatedData" border height="calc(100vh - 290px)">
        <el-table-column label="序号" align="center" width="70">
          <template #default="scope">
            <!-- 下标 + 1，计算公式：当前页数 * 每页大小 + 当前行的索引 + 1 --> {{
              (pageSearch.current - 1) * pageSearch.pageSize +
              scope.$index + 1
            }}
          </template>
        </el-table-column>
        <el-table-column prop="userName" label="操作工" align="center" min-width="200"/>
        <el-table-column prop="materialNo" label="物料编码" align="center" min-width="200" show-overflow-tooltip/>
        <el-table-column prop="equipmentName" label="设备名称" align="center" min-width="200" show-overflow-tooltip/>
        <el-table-column prop="noQualifiedCount" label="不合格产量" align="center" min-width="200"/>
        <el-table-column prop="orgName" label="工厂" align="center" min-width="200"/>
        <el-table-column prop="percentOfPass" label="合格率" align="center" min-width="200"/>
        <el-table-column prop="qualifiedCount" label="合格产量" align="center" min-width="200"/>
        <el-table-column prop="reportTime" label="报工时间" align="center" min-width="200"/>
        <el-table-column prop="scrapCount" label="报废数量" align="center" min-width="200"/>
        <el-table-column prop="scrapPercent" label="报废率" align="center" min-width="200"/>
        <el-table-column prop="shiftName" label="班次名称" align="center" min-width="200"/>
      </el-table>
      <div class="pagePagination">
        <el-pagination background v-model:current-page="pageSearch.current" v-model:page-size="pageSearch.pageSize"
                       :page-sizes="[10, 20, 50, 100, 200, 500]" layout="total, sizes, prev, pager, next"
                       :total="pageTotal"
                       @size-change="handleSizeChange" @current-change="handleCurrentChange"/>
      </div>
    </div>
  </div>
</template>
<script lang="ts" setup>
import {computed, reactive, ref} from 'vue';
import type {FormInstance, FormRules} from 'element-plus';
import {ElMessage, ElMessageBox} from 'element-plus';
import axiosInstance from '@/utils/http/api';
import {Delete, Edit, Plus, Refresh, Search} from '@element-plus/icons-vue';
import dayjs from 'dayjs';

onMounted(() => {
  // getList();
  getOption();
});


const pageSearch = ref({
  current: 1,
  pageSize: 10
});

const pageTotal = ref(null);//页面总数
const searchParam = ref({
  times: [
    dayjs().subtract(1, 'month').format('YYYY-MM-DD HH:mm:ss'),
    dayjs().format('YYYY-MM-DD HH:mm:ss')

  ],
});
const teamData = ref([]);//班组
const teamPeopleData = ref([]);//班组人员

const handleOrganizationChange = (value) => {

  searchParam.value.userId = [];
  searchParam.value.ids = [];
  if (!value) {
    return
  }
  axiosInstance.post('/api/basedataTeam/getAll', {
    organizationId: value
  })
      .then(res => {
        if (res.code !== 200) {
          ElMessage.error(res.message);
          return false;
        }
        teamData.value = res.data;
      });

}
const handleTeamChange = (value) => {

  searchParam.value.userId = [];
  if (!value) {
    return
  }
  axiosInstance.post('/api/teamUser/getAll', {
    teamId: value
  })
      .then(res => {
        if (res.code !== 200) {
          ElMessage.error(res.message);
          return false;
        }
        teamPeopleData.value = res.data;
      });

}


const paginatedData = ref([]);
const workRoomList = ref([]);//车间


const getOption = () => {
  axiosInstance.post('/api/basedataOrganization/getAll', {})
      .then(res => {
        if (res.code !== 200) {
          ElMessage.error(res.message);
          return false;
        }
        workRoomList.value = res.data;
      });

};

// 获取列表接口
const getList = () => {
  if (!searchParam.value.userId) {
    ElMessage.error('请选择人员');
    return
  }
  if (!searchParam.value.times) {
    ElMessage.error('请选择时间区间');
    return
  }
  axiosInstance.post('/api/reportWorkRecord/personYieldCount', {
    ...searchParam.value,
    startDate: searchParam.value?.times?.[0],
    endDate: searchParam.value?.times?.[1],
    times: undefined,
    ...pageSearch.value
  })
      .then(res => {
        if (res.code !== 200) {
          ElMessage.error(res.message);
          return false;
        }
        paginatedData.value = res.data.records;
        pageTotal.value = res.data.total;
      });
};

const handleSizeChange = (e) => {
  pageSearch.value.pageSize = e;
  getList();
};

const handleCurrentChange = (e) => {
  pageSearch.value.current = e;
  getList();
};

// 重置搜索条件
const resetSearch = () => {
  searchParam.value = {};
  pageSearch.value.current = 1;
  // current.value = 1
  getList();
};

</script>
<style scoped></style>
